package com.ruoyi.supplier.service.impl;

import java.util.List;

import com.ruoyi.common.constant.BusinessConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.NumberUtils;
import com.ruoyi.supplier.domain.*;
import com.ruoyi.supplier.service.ITbPriceService;
import com.ruoyi.supplier.service.ITbPriceStoreService;
import com.ruoyi.supplier.service.ITbProductPackService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.supplier.mapper.TbStoreOutMapper;
import com.ruoyi.supplier.service.ITbStoreOutService;

/**
 * 出库Service业务层处理
 *
 * @author ruoyi
 * @date 2021-03-07
 */
@Service
public class TbStoreOutServiceImpl implements ITbStoreOutService {
    private static final Logger log = LoggerFactory.getLogger(TbStoreOutServiceImpl.class);
    @Autowired
    private TbStoreOutMapper tbStoreOutMapper;

    @Autowired
    private ITbPriceService tbPriceServiceImpl;

    @Autowired
    private ITbPriceStoreService tbPriceStoreServiceImpl;
    @Autowired
    private ITbProductPackService tbProductPackServiceImpl;

    /**
     * 查询出库
     *
     * @param id 出库ID
     * @return 出库
     */
    @Override
    public TbStoreOut selectTbStoreOutById(Long id) {
        return tbStoreOutMapper.selectTbStoreOutById(id);
    }

    /**
     * 查询出库列表
     *
     * @param tbStoreOut 出库
     * @return 出库
     */
    @Override
    public List<TbStoreOut> selectTbStoreOutList(TbStoreOut tbStoreOut) {
        return tbStoreOutMapper.selectTbStoreOutList(tbStoreOut);
    }

    /**
     * 新增出库
     *
     * @param tbStoreOut 出库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbStoreOut(TbStoreOut tbStoreOut) {
        tbStoreOut.setCreateTime(DateUtils.getNowDate());
        tbStoreOut.setCreateBy(SecurityUtils.getUsername());
        int rows = tbStoreOutMapper.insertTbStoreOut(tbStoreOut);
        insertTbStoreOutDetail(tbStoreOut);
        return rows;
    }

    @Transactional
    @Override
    public int insertTbStoreOutQty(TbStoreOut tbStoreOut) {
        tbStoreOut.setCreateTime(DateUtils.getNowDate());
        tbStoreOut.setCreateBy(SecurityUtils.getUsername());
        int rows = tbStoreOutMapper.insertTbStoreOut(tbStoreOut);
        insertTbStoreOutDetailQty(tbStoreOut);
        return rows;
    }


    /**
     * 修改出库
     *
     * @param tbStoreOut 出库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbStoreOut(TbStoreOut tbStoreOut) {
        tbStoreOut.setUpdateTime(DateUtils.getNowDate());
        tbStoreOut.setUpdateBy(SecurityUtils.getUsername());
        tbStoreOutMapper.deleteTbStoreOutDetailByStoreOutId(tbStoreOut.getId());
        insertTbStoreOutDetail(tbStoreOut);
        return tbStoreOutMapper.updateTbStoreOut(tbStoreOut);
    }

    @Transactional
    @Override
    public int updateTbStoreOutQty(TbStoreOut tbStoreOut) {
        tbStoreOut.setUpdateTime(DateUtils.getNowDate());
        tbStoreOut.setUpdateBy(SecurityUtils.getUsername());
        tbStoreOutMapper.deleteTbStoreOutDetailByStoreOutId(tbStoreOut.getId());
        insertTbStoreOutDetailQty(tbStoreOut);
        return tbStoreOutMapper.updateTbStoreOut(tbStoreOut);
    }

    @Override
    public String selectNumber() {
        String number = tbStoreOutMapper.selectNumber();
        return NumberUtils.autoGenericNumber(number, BusinessConstants.CK);
    }


    /**
     * 批量删除出库
     *
     * @param ids 需要删除的出库ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbStoreOutByIds(Long[] ids) {
        tbStoreOutMapper.deleteTbStoreOutDetailByStoreOutIds(ids);
        return tbStoreOutMapper.deleteTbStoreOutByIds(ids);
    }

    /**
     * 删除出库信息
     *
     * @param id 出库ID
     * @return 结果
     */
    @Override
    public int deleteTbStoreOutById(Long id) {
        tbStoreOutMapper.deleteTbStoreOutDetailByStoreOutId(id);
        return tbStoreOutMapper.deleteTbStoreOutById(id);
    }


    /**
     * 新增出库明细信息
     *
     * @param tbStoreOut 出库对象
     */
    public void insertTbStoreOutDetail(TbStoreOut tbStoreOut) {
        List<TbStoreOutDetail> tbStoreOutDetailList = tbStoreOut.getTbStoreOutDetailList();
        Long id = tbStoreOut.getId();
        if (StringUtils.isNotNull(tbStoreOutDetailList)) {
            List<TbStoreOutDetail> list = new ArrayList<TbStoreOutDetail>();
            for (TbStoreOutDetail tbStoreOutDetail : tbStoreOutDetailList) {
                tbStoreOutDetail.setStoreOutId(id);
                list.add(tbStoreOutDetail);
            }
            if (list.size() > 0) {
                tbStoreOutMapper.batchTbStoreOutDetail(list);
            }
        }
    }

    @Transactional
    private void insertTbStoreOutDetailQty(TbStoreOut tbStoreOut) {
        List<TbStoreOutDetail> tbStoreOutDetailList = tbStoreOut.getTbStoreOutDetailList();
        Long id = tbStoreOut.getId();
        if (StringUtils.isNotNull(tbStoreOutDetailList)) {
            List<TbStoreOutDetail> list = new ArrayList<TbStoreOutDetail>();
            for (TbStoreOutDetail tbStoreOutDetail : tbStoreOutDetailList) {
                tbStoreOutDetail.setStoreOutId(id);
                list.add(tbStoreOutDetail);

                /**
                 * 库存明细
                 */
                TbPriceStore tbPriceStore = new TbPriceStore();
                tbPriceStore.setPriceId(tbStoreOutDetail.getPriceId());
                tbPriceStore.setStoreId(tbStoreOut.getStoreId());
                tbPriceStore.setsId(tbStoreOut.getsId());
                List<TbPriceStore> tbPriceStoreList = tbPriceStoreServiceImpl.selectTbPriceStoreList(tbPriceStore);
                Integer qtyDetail = tbStoreOutDetail.getQty();
                if (tbPriceStoreList != null && tbPriceStoreList.size() > 0) {
                    tbPriceStore.setId(tbPriceStoreList.get(0).getId());
                    TbPriceStore tbPStore = tbPriceStoreServiceImpl.selectTbPriceStoreById(tbPriceStore.getId());
                    Integer sc = tbPStore.getStoreCount();
                    Integer pc = tbPStore.getPackCount();

                    // 判断是否包装类型,如果是包装类型,按包装数量入库,否则按正常入库
                    // 但是如果正常入库，需要计算包装数量的增加
                    Long productId = tbStoreOutDetail.getProductId();
                    List<TbProductPack> tbProductPacks = tbProductPackServiceImpl.selectTbProductPackByProductId(productId);
                    if (tbStoreOutDetail.getIsPack().equalsIgnoreCase("Y")) {
                        if (pc - qtyDetail > -1) {
                            tbPriceStore.setPackCount(pc - qtyDetail);
                            if (tbProductPacks != null && tbProductPacks.size() > 0) {
                                // 每个商品的包装信息对应的商品有且只有一个
                                TbProductPack tbProductPack = tbProductPacks.get(0);
                                // 获取包装系数 , 包装系数必须为整数
                                Integer packModulus = tbProductPack.getPackModulus();
                                // 要扣减的包装数据不能小于 整箱数量 * 包装系数
                                // 扣减的数量 / 系数 = 要被扣减的整箱数量,数量大于整箱数量,取整箱数量,否则取 零
                                Integer cc =  qtyDetail / packModulus;
                                if(sc-cc>0){
                                    tbPriceStore.setStoreCount(sc - cc);
                                }else{
                                    tbPriceStore.setStoreCount(0);
                                }
                            }
                        } else {
                            throw new ServiceException("库存不能为负数", 500);
                        }
                    } else {
                        if (sc - qtyDetail > -1) {
                            tbPriceStore.setStoreCount(sc - qtyDetail);
                            if (tbProductPacks != null && tbProductPacks.size() > 0) {
                                // 每个商品的包装信息对应的商品有且只有一个
                                TbProductPack tbProductPack = tbProductPacks.get(0);
                                // 获取包装系数 , 包装系数必须为整数
                                Integer packModulus = tbProductPack.getPackModulus();
                                if(pc - (qtyDetail * packModulus) >0){
                                    tbPriceStore.setPackCount(pc - (qtyDetail * packModulus));
                                }else{
                                    tbPriceStore.setPackCount(0);
                                }
                            }

                        }

                    }
                    tbPriceStoreServiceImpl.updateTbPriceStore(tbPriceStore);

                }

            }
            if (list.size() > 0) {
                tbStoreOutMapper.batchTbStoreOutDetail(list);
            }
        }
    }
}
